﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DTO;
using BUS;

namespace GUI
{
    public partial class frmCapNhatThietBi : DevComponents.DotNetBar.Metro.MetroForm
    {
        public delegate void CapNhat(ThietBiDTO dto);
        public event CapNhat KhiCapNhat = null;

        public static string PathFile = "";
        public static string TenFile = "";

        public frmCapNhatThietBi()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmCapNhatThietBi_Load(object sender, EventArgs e)
        {
            ThietBiBUS bus = new ThietBiBUS();
            ThietBiDTO dto = bus.LayThongTinThietBi(frmMain.matb);
            txtTenThietBi.Text = dto.TenThietBi;
            //LoaiThietBiBUS ltbbus = new LoaiThietBiBUS();
            //List<LoaiThietBiDTO> dsltb = ltbbus.LayDanhSachLoaiThietBi();
            //cbLoaiThietBi.DataSource = dsltb;
            //cbLoaiThietBi.DisplayMember = "TenLoaiThietBi";
            //cbLoaiThietBi.ValueMember = "MaLoaiThietBi";
            //cbLoaiThietBi.SelectedValue = dto.MaLoaiThietBi;
            txtXuatXu.Text = dto.XuatXu;
            txtMoTa.Text = dto.ThongTinMoTa;
            Image img = Image.FromFile("Images/" + dto.HinhAnh);
            lbHinhAnh.BackgroundImage = img;
            //lbPath.Text = dto.HinhAnh; //luu path img cho tien viec capnhat, neu ko lam cach nay thi tao bien cuc bo de ham LayThongTin() su dug truy xuat dc
            TenFile = dto.HinhAnh;
            txtIMEI.Text = dto.IMEI;
            NhaCungCapBUS nccbus = new NhaCungCapBUS();
            List<NhaCungCapDTO> dsncc = nccbus.LayDanhSachNhaCungCap();
            //cbNhaCungCap.DataSource = dsncc;
            //cbNhaCungCap.DisplayMember = "TenNhaCungCap";
            //cbNhaCungCap.ValueMember = "MaNhaCungCap";
            //cbNhaCungCap.SelectedValue = dto.MaNhaCungCap;
            txtGiaNhap.Text = string.Format("{0:#,0.#}", dto.GiaNhap);
            txtGiaBan.Text = string.Format("{0:#,0.#}", dto.GiaBan);
            //txtGiaNhap.Text = dto.GiaNhap.ToString();
            //txtGiaBan.Text = dto.GiaBan.ToString(); 
            txtSoLuongTon.Text = dto.SoLuongTon.ToString();
        }

        private void lbHinhAnh_MouseHover(object sender, EventArgs e)
        {
            lbHinhAnh.BorderType = eBorderType.Etched;
        }

        private void lbHinhAnh_MouseLeave(object sender, EventArgs e)
        {
            lbHinhAnh.BorderType = eBorderType.None;
        }

        private void lbHinhAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.InitialDirectory = "D:";
            op.Filter = ".jpg, .png, .jpeg, .ico|*.jpg; *.png; *.jpeg; *.ico";
            if (op.ShowDialog() == DialogResult.OK)
            {
                PathFile = op.FileName;
                Image img = Image.FromFile(op.FileName);
                lbHinhAnh.BackgroundImage = img;
                TenFile = op.FileName.Substring(op.FileName.LastIndexOf("\\") + 1, op.FileName.Length - op.FileName.LastIndexOf("\\") - 1);
            }
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (txtTenThietBi.Text == "")
            {
                MessageBoxEx.Show("Tên thiết bị không được để trống!", "eMobile - Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenThietBi.Focus();
                return;
            }
            //if (txtGiaNhap.Text == "")
            //{
            //    MessageBoxEx.Show("Bạn chưa nhập giá nhập!", "eMobile - Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    txtTenThietBi.Focus();
            //    return;
            //}
            //if (txtGiaBan.Text == "")
            //{
            //    MessageBoxEx.Show("Bạn chưa nhập giá bán!", "eMobile - Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    txtTenThietBi.Focus();
            //    return;
            //}
            //if (Double.Parse(txtGiaNhap.Text) > Double.Parse(txtGiaBan.Text))
            if (Double.Parse(txtGiaNhap.Text.Replace(",", "")) > Double.Parse(txtGiaBan.Text.Replace(",", "")))
            {
                MessageBoxEx.Show("Giá bán phải cao hơn hoặc bằng giá nhập!", "eMobile - Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtGiaBan.Focus();
                return;
            }

            ThietBiDTO dto = LayThongTin();
            if (KhiCapNhat != null)
                KhiCapNhat(dto);
        }

        ThietBiDTO LayThongTin()
        {
            ThietBiDTO tbdto = new ThietBiDTO();
            tbdto.MaThietBi = frmMain.matb;
            tbdto.TenThietBi = txtTenThietBi.Text;
            //tbdto.MaLoaiThietBi = (int)cbLoaiThietBi.SelectedValue;
            tbdto.XuatXu = txtXuatXu.Text;
            tbdto.ThongTinMoTa = txtMoTa.Text;
            //tbdto.HinhAnh = lbPath.Text;
            tbdto.HinhAnh = TenFile;
            //tbdto.MaNhaCungCap = (int)cbNhaCungCap.SelectedValue;
            tbdto.IMEI = txtIMEI.Text;

            tbdto.GiaNhap = Double.Parse(txtGiaNhap.Text.Replace(",", ""));//thay , = "" de dua ve so ko co dau , moi Double.Parse dc
            tbdto.GiaBan = Double.Parse(txtGiaBan.Text.Replace(",", ""));
            //tbdto.GiaNhap = Double.Parse(txtGiaNhap.Text);
            //tbdto.GiaBan = Double.Parse(txtGiaBan.Text);
            tbdto.SoLuongTon = int.Parse(txtSoLuongTon.Text);
            return tbdto;
        }

        private void txtGiaBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaNhap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaNhap_Leave(object sender, EventArgs e)
        {
            if (txtGiaNhap.Text == "")
                txtGiaNhap.Text = "0";
            else
            {
                try//cho truog hop so nhap so qua lon
                {
                    txtGiaNhap.Text = string.Format("{0:#,0.#}", Double.Parse(txtGiaNhap.Text));
                }
                catch { }
            }
        }

        private void txtGiaBan_Leave(object sender, EventArgs e)
        {
            if (txtGiaNhap.Text == "")
                txtGiaNhap.Text = "0";
            else
            {
                try
                {
                    txtGiaBan.Text = string.Format("{0:#,0.#}", Double.Parse(txtGiaBan.Text));
                }
                catch { }
            }
        }

        private void txtSoLuongTon_Leave(object sender, EventArgs e)
        {
            if (txtSoLuongTon.Text == "")
                txtSoLuongTon.Text = "0";
        }

        private void txtGiaNhap_Enter(object sender, EventArgs e) //su kien click vao textbox hay tab vao textbox
        {
            txtGiaNhap.Text = txtGiaNhap.Text.Replace(",", "");
        }

        private void txtGiaBan_Enter(object sender, EventArgs e)
        {
            txtGiaBan.Text = txtGiaBan.Text.Replace(",", "");
        }
    }
}